package cn.wyh.security.config;

import cn.wyh.security.filter.TokenAuthFilter;
import cn.wyh.security.filter.TokenLoginFilter;
import cn.wyh.security.security.DefaultPasswordEncoder;
import cn.wyh.security.security.TokenLogoutHandler;
import cn.wyh.security.security.TokenManager;
import cn.wyh.security.security.UnAuthEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 * @author wyh
 * @version 1.0
 * @date 2021/10/5 16:24
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class TokenWebSecurityConfig extends WebSecurityConfigurerAdapter {

    private TokenManager tokenManager;
    private RedisTemplate redisTemplate;
    private DefaultPasswordEncoder defaultPasswordEncoder;
    private UserDetailsService userDetailsService;

    @Autowired
    public TokenWebSecurityConfig(UserDetailsService userDetailsService,DefaultPasswordEncoder defaultPasswordEncoder,
                                  RedisTemplate redisTemplate,TokenManager tokenManager){
        this.defaultPasswordEncoder=defaultPasswordEncoder;
        this.userDetailsService=userDetailsService;
        this.redisTemplate=redisTemplate;
        this.tokenManager=tokenManager;

    }

    //配置http组件
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.exceptionHandling()
                .authenticationEntryPoint(new UnAuthEntryPoint()) //设置未授权管理
                .and().csrf().disable()//关闭csrf
                .authorizeRequests()
                .anyRequest().authenticated() //拦截所有请求
                .and().logout().logoutUrl("/admin/acl/index/logout")//设置退出路径
                .addLogoutHandler(new TokenLogoutHandler(tokenManager,redisTemplate))//添加退出处理程序
                .and().addFilter(new TokenLoginFilter(redisTemplate,tokenManager,authenticationManager()))//添加登录过滤器
                .addFilter(new TokenAuthFilter(authenticationManager(),tokenManager,redisTemplate)).httpBasic();//添加权限过滤器
    }

    //配置访问数据库处理和加密格式
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(defaultPasswordEncoder);
    }

    //配置不拦截路径
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().mvcMatchers("/api/**");
    }
}
